package com.nft.service.school.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nft.constant.response.ResponseConstants;
import com.nft.paging.Paginator;
import com.nft.po.NftResponse;
import com.nft.service.school.entity.SchoolActivityEntity;
import com.nft.service.school.entity.SysUserEntity;
import com.nft.service.school.mapper.SchoolActivityMapper;
import com.nft.service.school.mapper.SysUserMapper;
import com.nft.service.school.req.SchoolActivityReq;
import com.nft.service.school.resp.SchoolActivityResp;
import com.nft.service.school.service.SchoolActivityService;
import com.nft.utils.query.QueryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SchoolActivityServiceImpl extends ServiceImpl<SchoolActivityMapper, SchoolActivityEntity> implements SchoolActivityService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public NftResponse<Paginator<SchoolActivityResp>> getActivityList(SchoolActivityReq query) {
        if (query.getPageNo() == null || query.getPageNo() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "入参页码错误", null);
        }
        if (query.getPageSize() == null || query.getPageSize() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "入参每页数量错误", null);
        }
        QueryWrapper<SysUserEntity> wrapperUser = new QueryWrapper<>();
        wrapperUser.eq(QueryUtil.camelToUnderline("phonenumber"), query.getPhone());
        wrapperUser.eq(QueryUtil.camelToUnderline("delFlag"),"0");
        wrapperUser.eq(QueryUtil.camelToUnderline("status"), "0");
        List<SysUserEntity> users = sysUserMapper.selectList(wrapperUser);
        List<Long> schoolIds = new ArrayList<>();
        for (SysUserEntity user:users) {
            schoolIds.add(user.getDeptId());
        }
        Paginator<SchoolActivityResp> paginator = new Paginator<SchoolActivityResp>(query.getPageNo(), query.getPageSize(), 0, new ArrayList<>());
        QueryWrapper<SchoolActivityEntity> wrapperQuery = new QueryWrapper<>();
        if (null!=schoolIds&&schoolIds.size()>0) {
            wrapperQuery.in("schoolId",schoolIds);
        }
        wrapperQuery.orderByDesc("create_time");
        Page<SchoolActivityEntity> resultPage = this.baseMapper.selectPage(new Page<>(query.getPageNo(),query.getPageSize()),wrapperQuery);
        List<SchoolActivityResp> pageResult = BeanUtil.copyToList(resultPage.getRecords(), SchoolActivityResp.class);
        paginator.setPageNo(resultPage.getCurrent());
        paginator.setPageSize(resultPage.getSize());
        paginator.setTotalDataNum(resultPage.getTotal());
        paginator.setPageData(pageResult);
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "获取成功", paginator);
    }

    @Override
    public NftResponse<SchoolActivityResp> getActivityInfoById(Long id) {
        SchoolActivityResp schoolActivityResp = null;
        SchoolActivityEntity actInfo = this.baseMapper.selectById(id);
        schoolActivityResp = BeanUtil.copyProperties(actInfo, SchoolActivityResp.class);
        if (schoolActivityResp == null) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "查无此公告", null);
        }
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "查询成功", schoolActivityResp);
    }
}
